package com.coincalf.task.event;

import com.alibaba.fastjson.JSONObject;
import com.coincalf.chan.feign.MessagingServiceClient;
import com.coincalf.feign.MarketServiceClient;
import com.coincalf.framework.enums.DepthMergeType;
import com.coincalf.framework.http.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 推送市场合并深度
 *
 * @author qiang
 */
@Component
@Slf4j
public class DepthEvent implements IEvent {

    @Autowired
    private MarketServiceClient marketServiceClient;
    @Autowired
    private MessagingServiceClient messagingServiceClient;

    /**
     * 推送市场合并深度
     */
    @Override
    public void handle() {
        marketServiceClient.tradeMarkets().forEach(market -> {
            for (DepthMergeType mergeType : DepthMergeType.values()) {
                String groupName = String.format("market.%s.depth.step%s", market.getSymbol(), mergeType.getValue()).toLowerCase();
                JSONObject body = new JSONObject();
                Response response = marketServiceClient.depth(market.getSymbol(), mergeType.getCode());
                body.put("tick", response.getData());
                messagingServiceClient.convertAndSendToGroup(groupName, body.toJSONString());
            }
        });
    }

}
